import java.time.LocalDateTime;
import java.util.Scanner;

public class LoginSystem {
    private static Scanner scanner = new Scanner(System.in);
    private static Cinema cinema = Cinema.getInstance();
    private static UserManagementSystem userSystem = cinema.getUserManagementSystem();
    public static void main(String[] args) {

        System.out.println("=== 影院管理系统 ===");
        cinema.initDefaultAdmin();
        while (true) {
            printMainMenu();
            int choice = getIntInput("请选择操作：");
            switch (choice) {
                case 1:
                    handleLogin();
                    break;
                case 2:
                    handleRegistration();
                    break;
                case 3:
                    System.out.println("退出系统");
                    return;
                default:
                    System.out.println("无效选择，请重新输入");
            }
        }
    }

    private static void printMainMenu() {
        System.out.println("\n1. 登录");
        System.out.println("2. 注册");
        System.out.println("3. 退出");
    }

    private static void handleLogin() {
        System.out.println("\n=== 登录 ===");
        System.out.print("用户名：");
        String username = scanner.nextLine();
        System.out.print("密码：");
        String password = scanner.nextLine();

        User user = userSystem.login(username, password);
        if (user != null) {
            enterSystem(user);
        }
    }

    private static void handleRegistration() {
        System.out.println("\n=== 注册 ===");
        System.out.println("请选择用户类型：");
        System.out.println("1. 管理员");
        System.out.println("2. 经理");
        System.out.println("3. 前台");
        int userType = getIntInput("请选择：");

        System.out.print("用户名：");
        String username = scanner.nextLine();
        System.out.print("密码：");
        String password = scanner.nextLine();

        switch (userType) {
            case 1:
                System.out.println("注册管理员需要现有管理员权限");
                System.out.print("管理员账号：");
                String adminUsername = scanner.nextLine();
                System.out.print("管理员密码：");
                String adminPassword = scanner.nextLine();

                User adminUser = userSystem.login(adminUsername, adminPassword);
                if (adminUser != null && adminUser.getRole().equals("Admin")) {
                    Admin admin = new Admin(username, password);
                    userSystem.registerUser(admin);
                } else {
                    System.out.println("权限不足，注册失败");
                }
                break;
            case 2:
                System.out.println("请联系管理员创建经理账号");
                break;
            case 3:
                System.out.println("请联系经理创建前台账号");
                break;
            default:
                System.out.println("无效的用户类型");
        }
    }

    private static void enterSystem(User user) {
        System.out.println("\n=== 欢迎 " + user.getUsername() + " (" + user.getRole() + ") ===");

        switch (user.getRole()) {
            case "Admin":
                showAdminMenu((Admin) user);
                break;
            case "Manager":
                showManagerMenu((Manager) user);
                break;
            case "Reception":
                showReceptionMenu((Reception) user);
                break;
            default:
                System.out.println("未知用户类型");
        }
    }
    private static void showAdminMenu(Admin admin) {
        while (true) {
            System.out.println("\n=== 管理员菜单 ===");
            System.out.println("1. 创建放映厅");
            System.out.println("2. 创建经理");
            System.out.println("3. 创建前台");
            System.out.println("4. 查看所有放映厅");
            System.out.println("5. 查看所有用户");
            System.out.println("6. 返回登录");

            int choice = getIntInput("请选择操作：");
            switch (choice) {
                case 1:
                    System.out.print("放映厅名称：");
                    String hallName = scanner.nextLine();
                    int rows = getIntInput("座位行数：");
                    int cols = getIntInput("座位列数：");
                    admin.createHall(hallName, rows, cols);
                    System.out.println("创建成功，已返回");
                    break;
                case 2:
                    System.out.print("经理用户名：");
                    String managerUsername = scanner.nextLine();
                    System.out.print("经理密码：");
                    String managerPassword = scanner.nextLine();
                    admin.createManager(managerUsername, managerPassword);
                    break;
                case 3:
                    System.out.print("前台用户名：");
                    String receptionUsername = scanner.nextLine();
                    System.out.print("前台密码：");
                    String receptionPassword = scanner.nextLine();
                    admin.createReception(receptionUsername, receptionPassword);
                    break;
                case 4:
                    admin.viewHalls();
                    break;
                case 5:
                    admin.viewAllUsers();
                    break;
                case 6:
                    return;
                default:
                    System.out.println("无效选择");
            }
        }
    }

    private static void showManagerMenu(Manager manager) {
        while (true) {
            System.out.println("\n=== 经理菜单 ===");
            System.out.println("1. 添加电影");
            System.out.println("2. 删除电影");
            System.out.println("3. 查看所有排片");
            System.out.println("4. 查看正在上映的电影");
            System.out.println("5. 返回登录");

            int choice = getIntInput("请选择操作：");

            switch (choice) {
                case 1:
                    System.out.print("电影名称：");
                    String name = scanner.nextLine();
                    System.out.print("导演：");
                    String director = scanner.nextLine();
                    System.out.print("简介：");
                    String description = scanner.nextLine();
                    int duration = getIntInput("时长(分钟)：");
                    int rating = getIntInput("评分(1-10)：");
                    boolean showing = getIntInput("是否上映(1=是,0=否)：") == 1;

                    Movie movie = new Movie(name, director, description, duration, rating, showing);

                    System.out.print("输入演员姓名(输入空行结束)：");
                    String actor;
                    while (!(actor = scanner.nextLine()).isEmpty()) {
                        movie.addActor(actor);
                    }

                    manager.addMovie(movie);
                    break;
                case 2:
                    System.out.print("要删除的电影名称：");
                    String movieName = scanner.nextLine();
                    manager.deleteMovie(movieName);
                    break;
                case 3:
                    manager.viewSchedules();
                    break;
                case 4:
                    System.out.println("正在上映的电影：");
                    for (Movie m : cinema.getMovieManagementSystem().listAllShowingMovies()) {
                        System.out.println("- " + m.getName());
                    }
                    break;
                case 5:
                    return;
                default:
                    System.out.println("无效选择");
            }
        }
    }

    private static void showReceptionMenu(Reception reception) {
        while (true) {
            System.out.println("\n=== 前台菜单 ===");
            System.out.println("1. 查看排片");
            System.out.println("2. 查看座位");
            System.out.println("3. 预订座位");
            System.out.println("4. 返回登录");

            int choice = getIntInput("请选择操作：");

            switch (choice) {
                case 1:
                    reception.viewSchedules();
                    break;
                case 2:
                    System.out.print("电影名称：");
                    String movieName = scanner.nextLine();
                    System.out.print("放映时间(例如 2025-07-17T19:30): ");
                    LocalDateTime time = LocalDateTime.parse(scanner.nextLine());
                    reception.showSeats(movieName, time);
                    break;
                case 3:
                    System.out.print("电影名称：");
                    movieName = scanner.nextLine();
                    System.out.print("放映时间(例如 2025-07-17T19:30): ");
                    time = LocalDateTime.parse(scanner.nextLine());
                    int row = getIntInput("座位行号：") - 1;
                    int col = getIntInput("座位列号：") - 1;
                    System.out.print("顾客姓名：");
                    String customerName = scanner.nextLine();
                    reception.bookSeat(movieName, time, row, col, customerName);
                    break;
                case 4:
                    return;
                default:
                    System.out.println("无效选择");
            }
        }
    }

    private static int getIntInput(String prompt) {
        System.out.print(prompt);
        while (!scanner.hasNextInt()) {
            System.out.println("请输入有效的整数！");
            scanner.next();
            System.out.print(prompt);
        }
        int input = scanner.nextInt();
        scanner.nextLine();
        return input;
    }
}